package project.gui.objects;

import java.awt.Color;
import java.awt.Graphics;

import project.events.antEvents.FollowingExplorationAntMovedEvent;

import framework.events.Event;
import framework.gui.GUIObject;
import framework.layer.physical.position.ContinuousPosition;

public class GUIFollowingExplorationAnt extends GUIObject {

	private final static int radius = 3;
	private final static int diameter = 2 * radius;
	
	private ContinuousPosition position;

	public GUIFollowingExplorationAnt(int id, ContinuousPosition position) {
		
		super(id);
		this.position = position;
	}
	


	public ContinuousPosition getPosition() {
		return position;
	}

	@Override
	protected void drawProtected(Graphics g) {
		g.setColor(Color.green);
		g.drawOval(convertX(getPosition().getX()) - radius, convertY(getPosition().getY()) - radius, diameter, diameter);
		g.setColor(Color.black);
	}

	@Override
	public boolean isConnectedOk() {
		return hasPosition();
	}

	public boolean hasPosition() {
		return position != null;
	}

	private void setPosition(ContinuousPosition pos) {
		this.position = pos;
		
	}


	@Override
	public void handleEvent(Event event) {
		if(event.getClass().equals(FollowingExplorationAntMovedEvent.class)){
			FollowingExplorationAntMovedEvent antMovedEvent = (FollowingExplorationAntMovedEvent) event;
			if(antMovedEvent.getId() == getId()){
				setPosition(antMovedEvent.getAnt().getLocation().getPosition());				
			}

		} 
		else {
			throw new IllegalStateException();
		}
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getId();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GUIFollowingExplorationAnt other = (GUIFollowingExplorationAnt) obj;
		if (getId() != other.getId())
			return false;
		return true;
	}



	@Override
	public void registerEvents() {
		getGui().getDispatcher().register(FollowingExplorationAntMovedEvent.class, this);
		
	}
}
